#include "tool_camera.h"
#include <QDebug>

QImage VideoframeProvider::ms_image;
/*!
 * \brief 构造函数，获取相机设备列表，实例化Videoframecaputer类用来抓取取景器帧数据
 * \param parent
 */
Tool_camera::Tool_camera(QObject *parent) : QObject(parent)
{
    info=QCameraInfo::availableCameras();
    m_videoframecaputer=new Videoframecaputer();


#ifndef QT_NO_DEBUG
    qDebug()<<"Tool_camera::Tool_camera";
#endif

}

/*!
 * \brief 析构函数
 */
Tool_camera::~Tool_camera()
{
//    m_camera->deleteLater();
    if(m_camera!=nullptr){
        m_camera->deleteLater();
        m_camera=nullptr;
    }

    if(m_videoframecaputer!=nullptr){
        m_videoframecaputer->deleteLater();
        m_videoframecaputer=nullptr;
    }

}

/*!
 * \brief videoframecaputer类控制发射newVideoFrame()信号
 */
void Tool_camera::emitsignalnewVideoFrame(QImage img)
{
    emit Tool_camera::newVideoFrame(img);
}


/*!
 * \brief 外部控制用来开启相机
 * \param deviceId  设备索引
 */
void Tool_camera::strartCamera(int deviceId)
{
    if(!info.isEmpty()){
        m_camera=new QCamera(info[deviceId]);

        //    m_camera->setViewfinder();

        m_camera->setViewfinder(m_videoframecaputer);

        m_camera->start();

        connect(m_videoframecaputer, &Videoframecaputer::newVideoFrame, this, &Tool_camera::newVideoFrame);

#ifndef QT_NO_DEBUG
        qDebug()<<"Tool_camera::strartCamera";
#endif
    }
    else{
#ifndef QT_NO_DEBUG
        qDebug()<<"Tool_camera::strartCamera"<<"deviceInfo  is  Empty";
#endif
    }


}
/*!
 * \brief 外部控制用来关闭相机
 */
void Tool_camera::closeCamera()
{
    if(m_camera!=nullptr){
        m_camera->stop();
        m_camera->deleteLater();
        m_camera=nullptr;
    }
}

/*!
 * \brief Videoframecaputer::Videoframecaputer
 * \param parent
 */
Videoframecaputer::Videoframecaputer(QObject *parent)
{

}


/*!
 * \brief Videoframecaputer::supportedPixelFormats
 * \param handleType
 * \return
 */
QList<QVideoFrame::PixelFormat> Videoframecaputer::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
{
    Q_UNUSED(handleType);
    return QList<QVideoFrame::PixelFormat>()
            << QVideoFrame::Format_ARGB32
            << QVideoFrame::Format_ARGB32_Premultiplied
            << QVideoFrame::Format_RGB32
            << QVideoFrame::Format_RGB24
            << QVideoFrame::Format_RGB565
            << QVideoFrame::Format_RGB555
            << QVideoFrame::Format_ARGB8565_Premultiplied
            << QVideoFrame::Format_BGRA32
            << QVideoFrame::Format_BGRA32_Premultiplied
            << QVideoFrame::Format_BGR32
            << QVideoFrame::Format_BGR24
            << QVideoFrame::Format_BGR565
            << QVideoFrame::Format_BGR555
            << QVideoFrame::Format_BGRA5658_Premultiplied
            << QVideoFrame::Format_AYUV444
            << QVideoFrame::Format_AYUV444_Premultiplied
            << QVideoFrame::Format_YUV444
            << QVideoFrame::Format_YUV420P
            << QVideoFrame::Format_YV12
            << QVideoFrame::Format_UYVY
            << QVideoFrame::Format_YUYV
            << QVideoFrame::Format_NV12
            << QVideoFrame::Format_NV21
            << QVideoFrame::Format_IMC1
            << QVideoFrame::Format_IMC2
            << QVideoFrame::Format_IMC3
            << QVideoFrame::Format_IMC4
            << QVideoFrame::Format_Y8
            << QVideoFrame::Format_Y16
            << QVideoFrame::Format_Jpeg
            << QVideoFrame::Format_CameraRaw
            << QVideoFrame::Format_AdobeDng
            <<QVideoFrame::Format_ARGB32;
}


/*!
 * \brief 重载，用来抓取获得的视频帧
 * \param frame   抓取获得的视频帧信息
 * \return
 */
bool Videoframecaputer::present(const QVideoFrame &frame)
{
#ifndef QT_NO_DEBUG
    qDebug()<<"Videoframecaputer::present"<<frame;
#endif

    if (frame.isValid()) {
//            QVideoFrame cloneFrame(frame);
//            cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
//            const QImage image(cloneFrame.bits(),
//                               cloneFrame.width(),
//                               cloneFrame.height(),
//                               QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat()));

//        videof

        QImage  img=QImage();
        img=frame.image();
//        VideoframeImageProvider::setImage(img);

        VideoframeProvider::setImage(img);


            emit newVideoFrame(img);
//            cloneFrame.unmap();

//        Tool_camera::emitsignalnewVideoFrame();
            return true;
        }
        return false;
}

/*!
 * \brief VideoframeProvider::VideoframeProvider
 */
VideoframeProvider::VideoframeProvider()
    :QQuickImageProvider(QQuickImageProvider::Pixmap)
{

}


/*!
 * \brief 重载，qml以此函数为接口获取图像数据
 * \param id
 * \param size
 * \param requestedSize
 * \return
 */
QPixmap VideoframeProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
    QPixmap  pixmap;


    return pixmap.fromImage(ms_image);
}

/*!
 * \brief 为图像提供者提供图像数据
 * \param img
 */
void VideoframeProvider::setImage(const QImage img)
{
    ms_image=img;
}
